'use strict';
const { Service } = require('egg');
const ModelName = 'Question'
class RouterService extends Service {
  async getPage(where, page, pageSize) {
    return this.ctx.model[ModelName].findAndCountAll({
        where: {
            ...where,
            delete_flag: 0,
        },
      offset: pageSize * (page - 1),
      limit: pageSize,

        order:[
            // id 降序
            ['created_time','DESC']
        ],
        include: [
            {
                model: this.ctx.model.QuestionOption,
                where: {
                    delete_flag: 0
                },
                required: false
            },
            {
                model: this.ctx.model.CourseCategories,
                where: {
                    delete_flag: 0
                },
                required: false
            },
            {
                model: this.ctx.model.Course,
            }
        ]
    });
  }
  // 所有列表
  async allList() {
    return this.ctx.model[ModelName].findAll({
      where: {
        delete_flag: 0
      },
      order:[
        // id 降序
          ['created_time','DESC']
      ],

    });
  }
  async findOne(id) {
    return this.ctx.model[ModelName].findOne({
      where: {
        id,
      },
    });
  }
  async create(data) {
      let options = data.options
      if (!options || options.length < 2) {
            throw new Error('选项至少两个')
      }
      const insertRes = await this.ctx.model[ModelName].create(data);
        if (insertRes) {
            let questionId = insertRes.id
            // 更新的选项，增加的选项
            const addList = options.filter(item => !item.id)
            // 更新的选项
            const updateList = options.filter(item => item.id)
            for (let i = 0; i < addList.length; i++) {
                const addItem = addList[i]
                await this.ctx.model.QuestionOption.create({
                    question_id: questionId,
                    ...addItem
                })
            }
            for (let i = 0; i < updateList.length; i++) {
                const updateItem = updateList[i]
                await this.ctx.model.QuestionOption.update(updateItem, {
                    where: {
                        id: updateItem.id
                    }
                })
            }
        }
    return
  }
  async update(id, data) {
      let questionId = id
      let options = data.options || []
      // 更新的选项，增加的选项
      const addList = options.filter(item => !item.id)
      // 更新的选项
      const updateList = options.filter(item => item.id)
      for (let i = 0; i < addList.length; i++) {
          const addItem = addList[i]
          await this.ctx.model.QuestionOption.create({
              question_id: questionId,
              ...addItem
          })
      }
      for (let i = 0; i < updateList.length; i++) {
          const updateItem = updateList[i]
          await this.ctx.model.QuestionOption.update(updateItem, {
              where: {
                  id: updateItem.id
              }
          })
      }
    return await this.ctx.model[ModelName].update(data, { where: { id } });
  }
  async deleteById(id) {
      console.log('+_+++++++++++++', id)
    return await this.ctx.model[ModelName].update({
        delete_flag: 1
    }, { where: { id } });
  }
}

module.exports = RouterService;
